/*
 * Copyright (C) 2015 Federico Iosue (federico.iosue@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.yitongmao.common.reminder;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.yitongmao.common.UrlConstants;


public class ReminderHelper {

    public static void addReminder(Context context, ReminderBean reminderBean) {
        if (reminderBean.getAlarm() > 0) {
            addReminder(context, reminderBean, reminderBean.getAlarm());
        }
    }

    public static void addReminder(Context context, ReminderBean reminderBean, long reminder) {
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra(UrlConstants.INTENT_NOTE, reminderBean);
        PendingIntent sender = PendingIntent.getBroadcast(context, reminderBean.getFileID(), intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            am.setExact(AlarmManager.RTC_WAKEUP, reminder, sender);
        } else {
            am.set(AlarmManager.RTC_WAKEUP, reminder, sender);
        }
    }

    /**
     * Checks if exists any reminder for given note
     */
    public static boolean checkReminder(Context context, ReminderBean reminderBean) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, reminderBean.getFileID(), new Intent(context, AlarmReceiver
                .class), PendingIntent.FLAG_NO_CREATE);
        return pendingIntent != null;
    }

    public static void removeReminder(Context context, ReminderBean reminderBean) {
//        if (!TextUtils.isEmpty(note.getAlarm())) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent p = PendingIntent.getBroadcast(context, reminderBean.getFileID(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
        am.cancel(p);
        p.cancel();
//        }
    }


//	public static void showReminderMessage(String reminderString) {
//		if (reminderString != null) {
//			long reminder = Long.parseLong(reminderString);
//			if (reminder > Calendar.getInstance().getTimeInMillis()) {
//				new Handler(OmniNotes.getAppContext().getMainLooper()).post(() -> Toast.makeText(OmniNotes
//								.getAppContext(),
//						OmniNotes.getAppContext().getString(R.string.alarm_set_on) + " " + DateHelper.getDateTimeShort
//								(OmniNotes.getAppContext(), reminder), Toast.LENGTH_LONG).show());
//			}
//		}
//	}
}
